package cn.one.ball.controller;

import cn.one.ball.entity.Banner;
import cn.one.ball.entity.BannerTwo;
import cn.one.ball.mapper.BannerMapper;
import cn.one.ball.mapper.BannerTwoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/bannerTwo/")
public class BannerTwoController {
    @Autowired(required = false)
    BannerTwoMapper mapper;
    @RequestMapping("select")
    public List<BannerTwo> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(int id){
        BannerTwo banner = mapper.selectById(id);
        new File("d:/files"+banner.getUrl()).delete();
    }
    @RequestMapping("insert")
    public void insert(String url){
        System.out.println("url = " + url);
        mapper.insert(url);
    }
    @RequestMapping("selectById")
    public BannerTwo selectById(int id){
        return mapper.selectById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody BannerTwo banner){
        BannerTwo oldBanner = mapper.selectById(banner.getId());
        //删除原来的图片文件
        new File("d:/files"+oldBanner.getUrl()).delete();
        mapper.update(banner);
    }
}
